package butines.core.gui.style;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import butines.core.GameImage;

public class GameLabel {

	private static Font font = new Font("Monospaced", Font.BOLD, 18);
	
	private static GameFont[] pallete = {
		new GameFont(font, new Color(51, 51, 51)), // BLACK
		new GameFont(font, Color.RED), // RED
		new GameFont(font, Color.GREEN), // GREEN
		new GameFont(font, Color.YELLOW), // YELLOW
		new GameFont(font, Color.BLUE), // BLUE
		new GameFont(font, new Color(0, 255, 255)), // LIGHT BLUE
		new GameFont(font, new Color(255, 0, 255)), // PINK
		new GameFont(font, Color.WHITE), // WHITE
		
		new GameFont(font, Color.BLACK, new Color(110, 110, 110), Color.BLACK, 2, 2), // BLACK
		new GameFont(font, new Color(110, 0, 0), Color.RED, Color.BLACK, 2, 2), // RED
		new GameFont(font, new Color(0, 110, 0), Color.GREEN, Color.BLACK, 2, 2), // GREEN
		new GameFont(font, new Color(110, 110, 0), Color.YELLOW, Color.BLACK, 2, 2), // YELLOW
		new GameFont(font, new Color(0, 0, 110), Color.BLUE, Color.BLACK, 2, 2), // BLUE
		new GameFont(font, new Color(0, 110, 110), new Color(0, 255, 255), Color.BLACK, 2, 2), // LIGHT BLUE
		new GameFont(font, new Color(110, 20, 110), new Color(255, 40, 255), Color.BLACK, 2, 2), // PINK
		new GameFont(font, new Color(225, 225, 225), new Color(240, 240, 240), Color.BLACK, 2, 2), // WHITE
		
		new GameFont(font, new Color(95, 51, 0, 245), new Color(247, 227, 35, 245), Color.BLACK, 2, 2), // Yellow
		new GameFont(font, new Color(100,149,237), new Color(240, 240, 240), Color.BLACK, 2, 2), // BLUE

	};
	
	public static String escape(String str) {
		return str == null ? null : str.replaceAll("\\[[^\\]]*\\]", "");
	}
	
	public static BufferedImage createImage(String str) {
		return createImage(str, pallete);
	}
	
	public static BufferedImage createImage(String str, GameFont[] pallete) {
		if (pallete == null) {
			pallete = GameLabel.pallete;
		}
		GameFont font = pallete[0];
		int w = font.getWidth(escape(str));
		int h = font.getLineHeight();
		BufferedImage image = GameImage.createCompatibleImage(w, h, Transparency.BITMASK);
		Graphics2D g2d = (Graphics2D) image.getGraphics();
		render(g2d, 0, 0, str);
		g2d.dispose();
		return image;
	}
	
	public static void render(Graphics2D g2d, float sx, float sy, String str) {
		render(g2d, sx, sy, str, pallete);
	}
	
	public static void render(Graphics2D g2d, float sx, float sy, String str, GameFont[] pallete) {
		if (pallete == null) {
			pallete = GameLabel.pallete;
		}
		int color = 0;
		StringBuilder buff = new StringBuilder();
		boolean escape = false;
		for (char ch : str.toCharArray()) {
			if (ch == '[') {
				if (buff.length() > 0) {
					String label = buff.toString();
					buff.setLength(0);
					pallete[color].render(g2d, sx, sy, label);
					sx += pallete[color].getWidth(label);
				}
				escape = true;
				continue;
			}
			if (escape && ch == ']') {
				try {
					color = 0;
					color = Integer.parseInt(buff.toString());
					color = Math.max(0, Math.min(color, pallete.length - 1));
				} catch (NumberFormatException e) {
					// continue;
				}
				buff.setLength(0);
				escape = false;
				continue;
			}
			buff.append(ch);
		}
		if (buff.length() > 0) {
			String label = buff.toString();
			buff.setLength(0);
			pallete[color].render(g2d, sx, sy, label);
			sx += pallete[color].getWidth(label);
		}
	}
		
}
